﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _9_1_Template
{
    public class Calculate
    {
        public static int Add(int a, int b)
        {
            return a + b;
        }

        public static float Add(float a, float b)
        {
            return a + b;
        }
    }

    //where TKey : TValue  TKey 和 TValue 是同一类型
    //where TKey : IComparable TKey 必须实现IComparable接口
    //where TKey : new()   TKey 类型必须有默认构造函数
    //where TKey : new() where TValue : new() 多个T一起约束
    //where TKey : struct where TValue : class  约束是值类型还是引用类型
    //where TKey : class, new()  多个条件约束一个类型，如果要约束new必须放在约束的最后


    public struct TestStruct<T, U>
    {
        public T t;
        public U u;
    }

    public interface TestInterface<T> where T : Animal
    {
        bool Compare(T t1, T t2);
        T GetAnimal();

        T Animals { get;}
    }

    public class TestFunc
    {
        public T GetDefault<T>()
        {
            return default(T);
        }

    }


    public class TestTemplate<TKey, TValue> where TKey : class, new() 
    {
        public TestTemplate()
        {
            Key = default(TKey);
            Value = default(TValue);
        }
        public TestTemplate(TKey key, TValue value) 
        {
            Key = key;
            Value = value;
        }

        public TValue Value;
        public TKey Key;

        public override string ToString()
        {
            return "[" + Key + ", " + Value + "]";
        }
    }

    public class Animal
    {
    }
    public class Cow : Animal
    {
    }
    public class Chicken : Animal
    {
    }

    class Program
    {
        static void Main(string[] args)
        {
            KeyValuePair<string, int> kvp = new KeyValuePair<string, int>();
            Console.WriteLine(kvp);

            TestTemplate<int, int> ti = new TestTemplate<int, int>();
            Console.WriteLine(ti);
            ti.Value = 123;
            ti.Key = 1;
            Console.WriteLine(ti);

            TestTemplate<float, float> tf = new TestTemplate<float, float>();
            tf.Key = 123.0f;
            tf.Value = 123.0f;


            Calculate.Add(123, 456);

            Calculate.Add(123.0f, 456.0f);

            Console.ReadKey();
        }
    }
}
